package com.ezzet.eulou.services;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.net.Uri;

import java.io.FileInputStream;
import java.io.IOException;

import com.ezzet.eulou.R;

public class AudioPlayer {

	static final String LOG_TAG = AudioPlayer.class.getSimpleName();

	private Context mContext;

	private MediaPlayer mPlayer;

	private AudioTrack mProgressTone;

	private final static int SAMPLERATE = 16000;

	public AudioPlayer(Context context) {
		this.mContext = context.getApplicationContext();
	}

	public void playRingtone() {
		AudioManager audioManager = (AudioManager) mContext
				.getSystemService(Context.AUDIO_SERVICE);

		// Honour silent mode
		switch (audioManager.getRingerMode()) {
		case AudioManager.RINGER_MODE_NORMAL:
			mPlayer = new MediaPlayer();
			mPlayer.setAudioStreamType(AudioManager.STREAM_RING);

			try {
				mPlayer.setDataSource(
						mContext,
						Uri.parse("android.resource://"
								+ mContext.getPackageName() + "/"
								+ R.raw.phone_loud1));
				mPlayer.prepare();
			} catch (IOException e) {
				mPlayer = null;
				return;
			}
			mPlayer.setLooping(true);
			mPlayer.start();
			break;
		}
	}

	public void stopRingtone() {
		if (mPlayer != null) {
			mPlayer.stop();
			mPlayer.release();
			mPlayer = null;
		}
	}

	public void playProgressTone() {
		stopProgressTone();
		try {
			mProgressTone = createProgressTone(mContext);
			mProgressTone.play();
		} catch (IOException e) {
		}
	}

	public void stopProgressTone() {
		if (mProgressTone != null) {
			mProgressTone.stop();
			mProgressTone.release();
			mProgressTone = null;
		}
	}

	private static AudioTrack createProgressTone(Context context)
			throws IOException {
		AssetFileDescriptor fd = context.getResources().openRawResourceFd(
				R.raw.progress_tone);
		int length = (int) fd.getLength();

		AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
				SAMPLERATE, AudioFormat.CHANNEL_OUT_MONO,
				AudioFormat.ENCODING_PCM_16BIT, length, AudioTrack.MODE_STATIC);

		byte[] data = new byte[(int) length];
		readFileToBytes(fd, data);

		audioTrack.write(data, 0, data.length);
		audioTrack.setLoopPoints(0, data.length / 2, 30);

		return audioTrack;
	}

	private static void readFileToBytes(AssetFileDescriptor fd, byte[] data)
			throws IOException {
		FileInputStream inputStream = fd.createInputStream();

		int bytesRead = 0;
		while (bytesRead < data.length) {
			int res = inputStream.read(data, bytesRead,
					(int) (data.length - bytesRead));
			if (res == -1) {
				break;
			}
			bytesRead += res;
		}
	}
}
